//
//  LZLoginRegisterViewController.m
//  LZHundredJie
//
//  Created by apple on 16/4/1.
//  Copyright © 2016年 m14a.cn. All rights reserved.
//

#import "LZLoginRegisterViewController.h"
#import "LZLoginRegisterView.h"
#import "LZFastLoginView.h"

@interface LZLoginRegisterViewController ()

/** 中间的view*/
@property (weak, nonatomic) IBOutlet UIView *middleView;
/** 底部的view*/
@property (weak, nonatomic) IBOutlet UIView *bottomView;
/** 中间的view的左侧约束*/
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leadCons;

@end

@implementation LZLoginRegisterViewController

/*
 1.如果一个控件从xib加载,必须固定尺寸
 
 2.不要在viewDidLoad设置子控件位置,应该在viewDidLayoutSubviews布局子控件
 */

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1.创建loginView登录视图
    LZLoginRegisterView *loginView = [LZLoginRegisterView loginView];
    // 2.创建registerView登录视图
    LZLoginRegisterView *registerView = [LZLoginRegisterView registerView];
                                    
    [self.middleView addSubview:loginView];
    [self.middleView addSubview:registerView];
    
    // 3.创建底部的view
    LZFastLoginView *fastView = [LZFastLoginView fastLoginView];
    [self.bottomView addSubview:fastView];
}

// 执行约束,里面尺寸才是最准确
- (void)viewDidLayoutSubviews
{
    // 1.1拿到loginView对象
    LZLoginRegisterView *loginView = self.middleView.subviews[0];
    // 1.2设置尺寸
    loginView.frame = CGRectMake(0, 0, self.middleView.width * 0.5, self.middleView.height);
    
    // 2.1拿到registerView对象
    LZLoginRegisterView *registerView = self.middleView.subviews[1];
    // 2.2设置尺寸
    registerView.frame = CGRectMake(self.middleView.width * 0.5, 0, self.middleView.width * 0.5, self.middleView.height);
    
    // 3.1拿到fastView对象
    LZFastLoginView *fastView = self.bottomView.subviews[0];
    fastView.frame = self.bottomView.bounds;
}

// 点击关闭
- (IBAction)close {
    [self dismissViewControllerAnimated:YES completion:nil];
}

// 点击注册账号
- (IBAction)clickRegister:(UIButton *)button
{
    button.selected = !button.selected;
    
    self.leadCons.constant = self.leadCons.constant == 0 ? -ScreenW : 0;
    [UIView animateWithDuration:0.25 animations:^{
        [self.view layoutIfNeeded];
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end
